package com.sneakxy.mybase4j.platform.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.sneakxy.mybase4j.platform.beans.Account;
import com.sneakxy.mybase4j.platform.provider.IAccountProvider;
import com.sneakxy.mybase4j.platform.provider.IAccountRoleProvider;
import com.sneakxy.mybase4j.platform.provider.IRolePermProvider;
import com.sneakxy.mybase4j.shiro.bean.ShiroAccount;
import com.sneakxy.mybase4j.shiro.service.IShiroService;
import com.sneakxy.mybatis.commons.repository.query.domain.CriteriaBuilder;

/**
 * 认证实现
 * @author 潜行的青衣
 * @see
 */
@Service("shiroService")
public class SecurityShiroService implements IShiroService {

	@Reference(version = "1.0.0")
	@Autowired(required = false)
	@Qualifier("accountProvider")
	private IAccountProvider accountProvider;
	
	@Reference(version = "1.0.0")
	@Autowired(required = false)
	@Qualifier("accountRoleProvider")
	private IAccountRoleProvider accountRoleprovider;
	
	@Reference(version = "1.0.0")
	@Autowired(required = false)
	@Qualifier("rolePermProvider")
	private IRolePermProvider rolePermProvider;
	
	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.base.shiro.service.IShiroService#findShiroAccount(java.lang.Object, java.lang.String)
	 */
	@Override
	public ShiroAccount findShiroAccount(Object principal, String host) {
		CriteriaBuilder<Account> builder = new CriteriaBuilder<Account>(Account.class);
		builder.andEq("account", (String) principal);
		return accountProvider.findOne(builder.build());
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.base.shiro.service.IShiroService#findRoles(com.sneakxy.mybase4j.base.shiro.bean.ShiroAccount)
	 */
	@Override
	public List<String> findRoles(ShiroAccount account) {
		return accountRoleprovider.findRoles(((Account) account).getId());
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.base.shiro.service.IShiroService#findPermissions(com.sneakxy.mybase4j.base.shiro.bean.ShiroAccount, java.util.List)
	 */
	@Override
	public List<String> findPermissions(ShiroAccount account, List<String> roles) {
		return this.rolePermProvider.findPermissions(roles);
	}

}
